package com.rc.evcharger.rabbit;

import com.alibaba.fastjson.JSON;
import com.rc.evcharger.common.config.RabbitListenerConstant;
import com.rc.evcharger.common.mq.MqProducer;
import com.rc.evcharger.model.member.MemberAccountRecord;
import com.rc.evcharger.service.member.MemberRefundOrderService;
import com.rc.evcharger.vo.charge.CommandStopReq;
import com.rabbitmq.client.Channel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Create by Yang on 2021/11/9
 */
@Component
public class RefundReceiver {
    private static Logger logger = LogManager.getLogger(RefundReceiver.class);

    @Autowired
    private MemberRefundOrderService memberRefundOrderService;

    @Autowired
    private MqProducer mqProducer;

    /**
     * 额度变更
     *
     * @param msg
     * @param channel
     */
    @RabbitListener(queues = {RabbitListenerConstant.CHANGE_MEMBER_REFUND_ORDER_QUEUE})
    @RabbitHandler
    public void processChangeMemberRefundOrder(String msg, Channel channel) {
        logger.info("processChangeMemberRefundOrder  1收到了:{}", msg);
        try {

            //getRequestAttributesSafely();
            MemberAccountRecord memberAccountRecord = JSON.parseObject(msg, MemberAccountRecord.class);
            int i = memberRefundOrderService.updateByMemberAccountRecord(memberAccountRecord);

            logger.info("processChangeMemberRefundOrder  返回的结果:{},{}", i, memberAccountRecord.getMemberId());
            return;
        } catch (Exception e) {
            logger.error("原路退款额度变更失败:{},{}", msg);
            e.printStackTrace();
//            mqProducer.sendDelay(RabbitListenerConstant.CHANGE_MEMBER_REFUND_ORDER_QUEUE, msg, 10 * 1000);
            return;
        }
    }


    /**
     * 先充后付成功
     *
     * @param msg
     * @param channel
     */
    @RabbitListener(queues = {RabbitListenerConstant.MINI_PAYMENT_REFUND_POINTS_ORDER_QUEUE,RabbitListenerConstant.WX_PAYMENT_REFUND_POINTS_ORDER_QUEUE})
    @RabbitHandler
    public void processPaymentRefundPoints(String msg, Channel channel) {
        logger.info("processPaymentRefundPoints  1收到了:{}", msg);
        try {

            //getRequestAttributesSafely();
            CommandStopReq commandStopReq = JSON.parseObject(msg, CommandStopReq.class);
            System.out.println(commandStopReq.toString());
//            logger.info("processChangeMemberRefundOrder  返回的结果:{},{}", i,memberAccountRecord.getMemberId());
            return;
        } catch (Exception e) {
            logger.error("先充后付成功:{},{}", msg);
            e.printStackTrace();
            mqProducer.sendDelay(RabbitListenerConstant.MINI_PAYMENT_REFUND_POINTS_ORDER_QUEUE, msg, 10 * 1000);
            return;
        }
    }
}
